package com.mapbased.wfw.handler;

import org.dom4j.*;
import com.mapbased.wfw.*;
import com.mapbased.cc.SpringAppContext;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy_hs(Baily)
 * @version 1.0
 */
public class ConfigHandler
    extends PageHandler
{
    public ConfigHandler()
    {
        super();
    }

    PageLoader loader;

    /**
     * defaultAction
     *
     * @param context RenderContext
     * @todo Implement this com.mapbased.wfw.PageHandler method
     */



    public void removeAction(RenderContext ctx)
    {
        if(this.passPermission(ctx))
        {
            this.loader.getPages().remove(ctx.getSureSingleParam("pagename"));
        }
    }

    public void reconfigAction(RenderContext ctx)
    {
        if(this.passPermission(ctx))
        {
            Config c=ctx.getConfig();

            c.postLoad(loader);
            //  this.loader.s);


            this.loader.getPages().clear();
        }

    }

    public void clearAction(RenderContext ctx)
    {
        if(this.passPermission(ctx))
        {

            this.loader.getPages().clear();
        }

    }

    public void reloadAction(RenderContext ctx)
    {
        if(this.passPermission(ctx))
        {
            ((SpringAppContext)ctx.global.getAppContext()).reload();
            Config c=ctx.getConfig();

            c.postLoad(loader);

            this.loader.getPages().clear();

        }

    }

    public Object developeModeBinding(java.util.Map m)
    {
        return Boolean.valueOf(this.loader.isDevelopeMode());
    }

    public Object pagesBinding(java.util.Map m)
    {
        return this.loader.getPages().keySet();
    }

    protected boolean passPermission(RenderContext ctx)
    {
        if(ctx.getRemoteAddr().equals("127.0.0.1"))
        {
            return true;
        }
        Object o=ctx.global.getAppContext();
        if(o instanceof SpringAppContext)
        {

            SpringAppContext sac=(SpringAppContext)o;

            try
            {
                Object ssohelper=sac.getBean("ssoHelper");
                if(ssohelper != null)
                {
                    java.lang.reflect.Method m=ssohelper.getClass().getMethod(
                        "getUserAutoRedirect",new Class[]
                        {ctx.getClass()});
                    Object user=m.invoke(ssohelper,new Object[]
                                         {ctx});

                    java.lang.reflect.Method isRoot=user.getClass().
                        getMethod(
                            "isRoot",new Class[0]);
                    Object root=isRoot.invoke(user,new Object[0]);
                    if(root instanceof Boolean)
                    {
                        return((Boolean)root).booleanValue();
                    }
                }
            }
            catch(Exception e)
            {
                ctx.putError("error",e.toString());
            }

        }
        ctx.putError("localhostneeded",
                     "Access denied: you need to perform this action on localhost");
        return false;
    }

    public void submitAction(RenderContext ctx)
    {
        if(this.passPermission(ctx))
        {

            String s=ctx.getRequest().getParameter("developeMode");
            this.loader.setDevelopeMode(s != null && s.length() > 0);
        }
    }

    /**
     *
     * @param ele Element
     * @todo Implement this com.mapbased.wfw.PageHandler method
     */
    protected void fetchAttributs(Element ele)
    {
    }

    /**
     * init
     *
     * @param servletContext ServletContext
     * @todo Implement this com.mapbased.wfw.PageHandler method
     */
    protected void init(GlobalRef global)
    {
        this.loader=global.getPageLoader();
    }
}
